package tools;

import com.myk.game.gobangchess.constants.PathCost;
import com.myk.game.gobangchess.utils.DateTimeUtil;
import com.myk.game.gobangchess.utils.FileUtils;
import com.myk.game.gobangchess.utils.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

public class LoginCountReport {

    static int rate = 1;
    static int countDays = 30;

    //输出最近30天登录人数统计图
    public static void main(String[] args) throws IOException {
        int maxValue = 0;
        List<DateLoginCount> dateLoginCountMap = new java.util.ArrayList<>();
        for (int i = 1; i <= countDays; i++) {
            LocalDateTime date = DateTimeUtil.minusDays(LocalDateTime.now(),countDays - i);
            String dateStr = DateTimeUtil.localDateTimeToStr(date, DateTimeUtil.DATE_FORMAT);
            String file = PathCost.GLOBAL_LOGINS_DATA_DIR + dateStr + ".txt";
            dateStr = dateStr.substring(5);
            if (!FileUtils.isFileExists(file)){
                dateLoginCountMap.add(new DateLoginCount(dateStr, 0));
            }
            else {
                String data = FileUtils.readFile2String(file, "GBK");
                if (StringUtils.isSpace(data)) {
                    dateLoginCountMap.add(new DateLoginCount(dateStr, 0));
                }
                else {
                    int value = data.split("，").length;
                    if (maxValue < value) {
                        maxValue = value;
                    }
                    dateLoginCountMap.add(new DateLoginCount(dateStr, value));
                }
            }
        }
        String outputFile = PathCost.TOOL_OUTPUT_DIR + "login_count_report.png"; //++最好重命名一下
        int width = 1920 * rate; //输出图片宽度1920
        int height= 1080 * rate; //输出图片高度1080
        float unitWidth = 1f * width / (countDays + 2); //单个日期宽度 (+2是多出2格等宽的边距)
        float unitHeight = 1f * height / (maxValue + 2); //每人数高度刻度 （+2是多出2格等宽的边距）
        BufferedImage image = new BufferedImage( width,  height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);//抗锯齿
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);
        //根据dateLoginCountMap中的数据绘制竖状图和折线图
        Font valueFont = new Font("黑体", Font.BOLD, (int) (unitWidth/3));
        Font dateFont = new Font("宋体", Font.PLAIN, (int) (unitWidth/4.2));
        Stroke stroke = new BasicStroke(2f);
        g2d.setStroke(stroke);
        for (int i = 0; i < dateLoginCountMap.size(); i++) {
            DateLoginCount dateLoginCount = dateLoginCountMap.get(i);
            float x = i * unitWidth + unitWidth;
            float itemHeight = dateLoginCount.getValue() * unitHeight;
            float y = height - unitHeight - itemHeight;
            //绘制条形图
            g2d.setColor(Color.BLUE);
            g2d.fillRect((int) (x + unitWidth/10), (int) y + 2, (int) (unitWidth - unitWidth / 20), (int) itemHeight + 2); //加2像素的边框
            //绘制折线图
            if (i > 0) {
                DateLoginCount lastDateLoginCount = dateLoginCountMap.get(i - 1);
                float lastX = (i - 1) * unitWidth + unitWidth/2 + unitWidth;
                float lastY = height - unitHeight - lastDateLoginCount.getValue() * unitHeight;
                g2d.setColor(Color.RED);
                g2d.fillOval((int) (lastX - unitWidth / 20), (int) (lastY - unitWidth / 20), (int) unitWidth / 10, (int) unitWidth / 10);
                g2d.drawLine((int) lastX, (int) lastY, (int) (x + unitWidth / 2), (int) y);
            }
            //在每一格最上面绘制登录人数
            g2d.setColor(Color.BLACK);
            g2d.setFont(valueFont);
            g2d.drawString(dateLoginCount.getValue() + "", (int) x + unitWidth / 2 - unitWidth / 10, y - unitHeight / 2 + unitWidth / 10);
            //在每一格最底下绘制日期
            g2d.setFont(dateFont);
            g2d.drawString(dateLoginCount.getDate(), (int) (x + unitWidth / 3.5),  height - unitHeight / 2);
        }
        g2d.dispose();
        File outputFileImage = new File(outputFile);
        ImageIO.write(image, "png", outputFileImage);
    }

    public static class DateLoginCount {
        private String date;
        private int value;

        public DateLoginCount(String date, int value) {
            this.date = date;
            this.value = value;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }
}
